package org.linlinjava.litemall.brand.web;

import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.service.LitemallAdService;
import org.linlinjava.litemall.db.service.LitemallServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * AD 是行业快报..也是商户上传的文档 地址
 */
@RestController
@RequestMapping("/brand/doc")
public class BrandDocController {
    private final Log log = LogFactory.getLog(BrandDocController.class);

    @Autowired
    private StorageService storageService;
    /**
     * 拦截请求头  以获取 转化成brandId
     */
    @Autowired
    private LitemallServiceService serviceService;
    @Autowired
    private LitemallAdService adService;
    //brand/doc/listNotice
    //根据商户ID 获得自己发布的行业信息DOC
    @GetMapping("/listNotice")
    public Object CompanyDocList(@LoginUser Integer id,String name,
                @RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "10") Integer limit,
                @Sort @RequestParam(defaultValue = "add_time") String sort,
                @Order @RequestParam(defaultValue = "desc") String order) {

        Integer brandId = serviceService.getBelong(id);
        System.out.println("商户中brandId>>>" + id);
        PageInfo<LitemallAd> adPageInfo = adService.queryByBrandId(name, page, limit, sort, order, brandId);
        return ResponseUtil.ok(adPageInfo);
    }


    @PostMapping("addDoc")
    public Object addDoc(@LoginUser Integer id,@RequestBody LitemallAd ad) {
        Integer brandId = serviceService.getBelong(id);
        LitemallAd AdDB = new LitemallAd();
        BeanUtils.copyProperties(ad,AdDB);
        adService.add(AdDB);
        return ResponseUtil.ok();
    }


//    @PostMapping("/add")
//    public Object addPeople(@RequestBody String data){
//        System.out.println("people>>" + data);
//        JSONObject jsonObject = JSONObject.parseObject(data);
//        Integer brandId = jsonObject.getInteger("brandId");
//        String name = jsonObject.getString("name");
//        String position = jsonObject.getString("position");
//        String desc = jsonObject.getString("desc");
//        String picUrl = jsonObject.getString("picUrl");
//
//        if(null == brandId || "".equals(brandId) ){ return ResponseUtil.fail(402,"请确认本公司的brandId");}
//        if(StringUtils.isEmpty(name) ){ return ResponseUtil.fail(402,"请确认员工姓名name");}
//        if(StringUtils.isEmpty(position)){ return ResponseUtil.fail(402,"请确认职位position");}
//        if(StringUtils.isEmpty(picUrl)){ return ResponseUtil.fail(402,"请上传图片");}
//
//    /*    StringUtils.isEmpty(brandPeople.getBrandId().toString())
//                || StringUtils.isEmpty(brandPeople.getName())
//                || StringUtils.isEmpty(brandPeople.getPosition())*/
//        LitemallBrandPeople brandPeople = new LitemallBrandPeople();
//        brandPeople.setBrandId(brandId);
//        brandPeople.setName(name);
//        brandPeople.setPosition(position);
//        brandPeople.setDesc(desc);
//        brandPeople.setPicUrl(picUrl);
//        brandPeopleService.addBrandPeople(brandPeople);
//        return ResponseUtil.ok();
//    }




}
